//
//  myViewController.m
//  pacc
//
//  Created by 测试 on 2018/6/15.
//  Copyright © 2018年 测试. All rights reserved.
//

#import "myViewController.h"
#import "userContentController.h"
#import "accountAndSafeController.h"
@interface myViewController ()
@property(nonatomic,strong)UIImageView * headerImageView;
@property(nonatomic,strong)UIButton * button;
@property(nonatomic,strong)UIButton * button3;
@property(nonatomic,strong)UILabel * hosNameLabel;
@property(nonatomic,strong)UILabel * useNameLabel;
@property(nonatomic,strong)UILabel * phoneNoLabel;
@property(nonatomic,copy)NSString * headUrl;
@property (copy, nonatomic)  NSString *useridImage;

@end

@implementation myViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViews];
    [self setSubViewData];
    //拿到图片
    UIImage * image = [UIImage imageNamed:@"touxiang_nan"];
    NSString *path_document = NSHomeDirectory();
    //设置一个图片的存储路径
    NSString *imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/touxiang.png"]];
    //把图片直接保存到指定的路径（同时应该把图片的路径imagePath存起来，下次就可以直接用来取）
    [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [self loadData];
    NSString *path_document = NSHomeDirectory();
    if (my_AppDelegate.userid) {
        NSString *imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@.png",my_AppDelegate.userid]];
        UIImage*image=[[UIImage alloc]initWithContentsOfFile:imagePath];
        if (image) {
            [_headerImageView setImage:image];
        }
    }else{
        NSString *imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/touxiang.png"]];
        UIImage*image=[[UIImage alloc]initWithContentsOfFile:imagePath];
        if (image) {
            [_headerImageView setImage:image];
        }
    }
    
}
-(void)viewDidDisappear:(BOOL)animated{
    _button.userInteractionEnabled = YES;
    _button3.userInteractionEnabled = YES;
}
-(void)setSubViews{
    UIView * headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headerView];
    UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ZTbigmargin, ZTbigmargin,headerView.height-ZTbigmargin*2, headerView.height-ZTbigmargin*2)];
    _headerImageView = headerImageView;
    headerImageView.layer.cornerRadius = headerImageView.height/2;
    headerImageView.layer.masksToBounds = YES;
    [headerView addSubview:headerImageView];
    [headerImageView setImage:[UIImage imageNamed:@"touxiang_nan"]];
    UILabel * userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(headerImageView.x+headerImageView.width+ZTbigmargin, ZTbigmargin, 100, 20)];
    [userNameLabel setTextColor:titleblackColor];
    [userNameLabel setFont:titlefont];
    _useNameLabel = userNameLabel;
    [headerView addSubview:userNameLabel];
    
    UILabel * phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(userNameLabel.x, headerImageView.y+headerImageView.height -20, 100, 20)];
    [phoneLabel setTextColor:titleGlayColor];
    [phoneLabel setFont:textlittlefont];
    _phoneNoLabel = phoneLabel;
    phoneLabel.centerY = headerImageView.centerY;
    phoneLabel.x = userNameLabel.x;
    [headerView addSubview:phoneLabel];
    
    UILabel *hosNamelabel = [[UILabel alloc]initWithFrame:CGRectMake(userNameLabel.x, headerImageView.y+headerImageView.height -20,  ScreenWidth-_headerImageView.width-30, 20)];
    _hosNameLabel = hosNamelabel;
    [hosNamelabel setFont:textlittlefont];
    [hosNamelabel setTextColor:titleGlayColor];
    [headerView addSubview:hosNamelabel];
    
    UIButton * button3= [[UIButton alloc]initWithFrame:headerView.bounds];
    _button3= button3;
    [button3 addTarget:self action:@selector(goAccount:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:button3];
    
    UIView * accountView = [[UIView alloc]initWithFrame:CGRectMake(0, headerView.y+headerView.height+ZTbigmargin, ScreenWidth, 50)];
    accountView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:accountView];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, accountView.height)];
    _button =button;
    [button setTitle:@"账号与安全" forState:UIControlStateNormal];
    [button setTitleColor:titleblackColor forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"jinru"] forState:UIControlStateNormal];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, ScreenWidth-button.imageView.frame.size.width, 0, 0)];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.titleEdgeInsets = UIEdgeInsetsMake(0, -ZTbigmargin, 0, 0);
    [button addTarget:self action:@selector(goBag:) forControlEvents:UIControlEventTouchDown];
    [accountView addSubview:button];
    
    UIView * bagView = [[UIView alloc]initWithFrame:CGRectMake(0, accountView.y+accountView.height+ZTbigmargin, ScreenWidth, 50)];
    bagView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bagView];
    
    UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, bagView.height)];
    [button2 setTitle:@"我的钱包" forState:UIControlStateNormal];
    [button2 setTitleColor:titleblackColor forState:UIControlStateNormal];
    [button2 setImage:[UIImage imageNamed:@"jinru"] forState:UIControlStateNormal];
    [button2 setImageEdgeInsets:UIEdgeInsetsMake(0, ScreenWidth-button.imageView.frame.size.width, 0, 0)];
    button2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button2.titleEdgeInsets = UIEdgeInsetsMake(0, -ZTbigmargin, 0, 0);
    [bagView addSubview:button2];
}
-(void)setSubViewData{
    NSString *path_document = NSHomeDirectory();
    NSString *imagePath = [path_document stringByAppendingString:@"/Documents/piccc.png"];
    UIImage *getimage2 = [UIImage imageWithContentsOfFile:imagePath];
    if (getimage2){
        [_headerImageView setImage:getimage2];
    }else{
        [_headerImageView setImage:[UIImage imageNamed:@"touxiang_nan"]];
    }
    NSLog(@"image2 is size %@",NSStringFromCGSize(getimage2.size));
    self.view.backgroundColor = baseBackgroundColor;
}
-(void)loadData{
    [_headerImageView setImage:[UIImage imageNamed:@"touxiang_nan"]];
    if (!my_AppDelegate.token) {
        return;
    }
    [NetWorking startPOSTRequestWithUrl:[NSString stringWithFormat:@"userget"] parameters:@{@"token":my_AppDelegate.token} logMes:@"用户信息" show:NO success:^(NSDictionary *data) {
        NSString * status = [NSString stringWithFormat:@"%@",[data objectForKey:@"status"]];
        if ([status isEqualToString:@"200"]) {
            NSDictionary * dic = [data objectForKey:@"data"];
            NSString *phone = [NSString stringWithFormat:@"%@",[dic objectForKey:@"phone"]];
            NSString *userName = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userName"]];
            NSString *hospitalName = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userDesc"]];
            NSString *myuesrid = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
            NSString *headUrl = [NSString stringWithFormat:@"%@",[dic objectForKey:@"headUrl"]];
            self->_headUrl = headUrl;
            UIImage * image = [tools getImageFromURL:headUrl];
            self->_useridImage = myuesrid;
            my_AppDelegate.userid= myuesrid;
            //拿到图片
            NSString *path_document = NSHomeDirectory();
            //设置一个图片的存储路径
            NSString *imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@.png",myuesrid]];
            //把图片直接保存到指定的路径（同时应该把图片的路径imagePath存起来，下次就可以直接用来取）
            [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self->_phoneNoLabel.text = phone;
                self->_useNameLabel.text = userName;
                self->_hosNameLabel.text = hospitalName;
                self->_headerImageView.image = image;
            });
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)goAccount:(UIButton*)sender{
    sender.userInteractionEnabled = NO;
    userContentController *vc =[[userContentController alloc]init];
    vc.returnValueBlock = ^(UIImage *image) {
        [self->_headerImageView setImage:image];
    };
    vc.userDesc = _hosNameLabel.text;
    vc.userName = _useNameLabel.text;
    vc.headUrl = _headUrl;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)goBag:(UIButton*)sender{
    sender.userInteractionEnabled = NO;
    accountAndSafeController * vc = [accountAndSafeController new];
    vc.phone = _phoneNoLabel.text;
    vc.userName = _useNameLabel.text;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
